cmake_minimum_required(VERSION 3.10)
project(simplelock VERSION 1.0.0 DESCRIPTION "系统级Raylib屏幕锁")

# 设置C标准（兼容Raylib）
# set(CMAKE_C_STANDARD 99)
set(CMAKE_C_STANDARD_REQUIRED ON)

# 手动配置Raylib路径（对应GCC的-I include/ 和 -L lib/）
set(RAYLIB_INCLUDE_DIR "${PROJECT_SOURCE_DIR}/include" CACHE PATH "Raylib头文件目录")
set(RAYLIB_LIB_DIR "${PROJECT_SOURCE_DIR}/lib" CACHE PATH "Raylib静态库目录")
set(RAYLIB_LIB_NAME "libraylib.a" CACHE STRING "Raylib静态库文件名")

# 验证Raylib头文件和库文件是否存在
if(NOT EXISTS "${RAYLIB_INCLUDE_DIR}/raylib.h")
    message(FATAL_ERROR "Raylib头文件未找到！路径: ${RAYLIB_INCLUDE_DIR}/raylib.h")
endif()
if(NOT EXISTS "${RAYLIB_LIB_DIR}/${RAYLIB_LIB_NAME}")
    message(FATAL_ERROR "Raylib静态库未找到！路径: ${RAYLIB_LIB_DIR}/${RAYLIB_LIB_NAME}")
endif()

# 安装路径配置（默认/usr/bin）
set(INSTALL_BIN_DIR "${CMAKE_INSTALL_PREFIX}/bin" CACHE PATH "可执行文件安装路径")
if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
    set(CMAKE_INSTALL_PREFIX "/usr" CACHE PATH "默认安装到系统目录/usr" FORCE)
endif()

# 可执行文件编译配置
add_executable(${PROJECT_NAME} simplelock.c)

# 关联头文件和库文件（模拟GCC参数）
target_include_directories(${PROJECT_NAME} PRIVATE ${RAYLIB_INCLUDE_DIR})
target_link_directories(${PROJECT_NAME} PRIVATE ${RAYLIB_LIB_DIR})
target_link_libraries(${PROJECT_NAME} PRIVATE "${RAYLIB_LIB_NAME}" m pam)

# 编译选项
target_compile_options(${PROJECT_NAME}
    PRIVATE
        -Wall
        -Wextra
        -Wpedantic
        -Werror=implicit-function-declaration
)

# Debug模式宏定义
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -DDEBUG_MODE")
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -O2")

# 安装规则
install(
    TARGETS ${PROJECT_NAME}
    DESTINATION ${INSTALL_BIN_DIR}
    PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE
                GROUP_READ GROUP_EXECUTE
                WORLD_READ WORLD_EXECUTE
)

# ===================== 简化的卸载功能（无需cmake_uninstall.cmake.in） =====================
if(NOT TARGET uninstall)
    add_custom_target(uninstall
        # 直接删除安装的可执行文件
        COMMAND ${CMAKE_COMMAND} -E remove "${INSTALL_BIN_DIR}/${PROJECT_NAME}"
        COMMENT "正在卸载 ${PROJECT_NAME}（路径：${INSTALL_BIN_DIR}/${PROJECT_NAME}）"
    )
endif()

# 编译状态提示
message(STATUS "=============================================")
message(STATUS "项目名称: ${PROJECT_NAME} ${PROJECT_VERSION}")
message(STATUS "安装路径: ${INSTALL_BIN_DIR}")
message(STATUS "编译模式: ${CMAKE_BUILD_TYPE}")
message(STATUS "=============================================")